﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CourseManager.Model.Data
{
	public class StudentDAO
	{
		public static void Add(string id, string name, string sex, string dept, int grade)
		{
			DBEntity ent = new DBEntity();
			ent.AddToTab_Student(new Tab_Student
			                     	{
			                     		studentId = id,
										name = name,
										sex = sex,
										dept = dept,
										grade = grade
			                     	});
			ent.SaveChanges();
		}

		public static void Remove(string id)
		{
			UserDAO.Remove(id);

			DBEntity ent = new DBEntity();

			Tab_Student student = (from _student in ent.Tab_Student
			                    where _student.studentId.Equals(id)
			                    select _student).FirstOrDefault();

			if (student != null)
			{
				ent.DeleteObject(student);
				ent.SaveChanges();
			}
		}


		public static Tab_Student FindById(String id)
		{
			DBEntity ent = new DBEntity();

			Tab_Student student = (from _student in ent.Tab_Student
								   where _student.studentId.Equals(id)
			                       select _student).FirstOrDefault();

			return student;
		}

		public static IEnumerable<Tab_Student> FindAll()
		{
			return from student in new DBEntity().Tab_Student select student;
		}

	}
}